<div id="tabs-1"
	style=" padding: 10px; width: 45%">
	<h3>Manage Categories</h3>
	<hr>
	<div id="categories">&nbsp;</div>
	<div id="cat_input">
		<input type="text" name="cat_in" id="cat_in"
			style="width: 500px; padding: 5px; margin-right: 10px;" />
		<button id="cat_sub" value="Add"
			style="padding: 5px; margin-right: 10px;">Add Category</button>
		<button id="cat_sub_update" value="Update"
			style="padding: 5px; margin-right: 10px;">Update Category</button>
	</div>

</div>

<script text="text/javascript">


//Categories
function loadCategories(){
	console.log("Loading categories...");
	$.getJSON('<?php print base_url(); ?>xmlrpc_client/get_categories',function(data){
		
		console.log("Categories loaded.."+data);
	    var text='<div class="tobjects">';
	    var i=1;
		$.each(data, function(key, val) {
		      text=text+'<ul catid="'+val.id+'" class="objdiv ui-widget ui-helper-clearfix">'
		      +'<li class="ui-state-default ui-corner-all editcat" ><span class="ui-icon ui-icon-pencil"></span></li>'
		      +'<li class="ui-state-default ui-corner-all delcat" ><span class="ui-icon ui-icon-trash"></span></li>'
		      +'<li class="namebox">'+val.name+'</li></ul>';
		      
		      i++;
		});	
		text=text+'</div>';
		$('#categories').html(text);	
		$('#cat_sub_update').hide();
		$('#cat_sub').show();
		$('#cat_in').val("")
		associateCategoryActions();

		
	});
	
}

function associateCategoryActions(){

	$('.editcat').each(function(){
		$(this).click(function(){

			inobj=$(this);
			ulobj=inobj.parent();
			nameobj=ulobj.children(".namebox");

			$('#cat_in').val(nameobj.text());
			$('#cat_sub_update').attr('catid',ulobj.attr('catid'));

			$('#cat_sub').hide();
			
			$('#cat_sub_update').show();
			
		});
	});
	
	$('.delcat').each(function(){
		$(this).click(function(){

			inobj=$(this);
			ulobj=inobj.parent();
			catid=ulobj.attr('catid');

			//Post and delete
			var dataString='&catid='+catid;

			$.ajax({
			type: "POST",
				url: "<?php print base_url(); ?>xmlrpc_client/delete_category",
				data: dataString,
				success : function(data){
					$.jGrowl("Category deleted.!");
					loadCategories();
				}
			});			
			
		});
	});

	$('#cat_sub').button().click(function(){
		var text=$('#cat_in').val();
		var catid=$(this).attr('catid');
		
		if(text != "" ){
			var dataString='&catid='+catid+'&name='+text;

				$.ajax({
				type: "POST",
					url: "<?php print base_url(); ?>xmlrpc_client/create_category",
					data: dataString,
					success : function(data){
						$.jGrowl("Category created.!");
						loadCategories();
					}
				});
			
		}

	});

	$('#cat_sub_update').button().click(function(){
		var text=$('#cat_in').val();
		var catid=$(this).attr('catid');
		
		if(text != "" ){
			var dataString='&catid='+catid+'&name='+text;

				$.ajax({
				type: "POST",
					url: "<?php print base_url(); ?>xmlrpc_client/update_category",
					data: dataString,
					success : function(data){
						$.jGrowl("Category updated.!");
						loadCategories();
					}
				});
		}

	});
	
}


$(document).ready(function(){

   //load objects
   	loadCategories();


   //load fields
	
});
</script>
